//
//  UITextField+DDFormatter.m
//  dinglc
//
//  Created by DD on 2018/1/24.
//  Copyright © 2018年 lg. All rights reserved.
//

#import "UITextField+LLFormatter.h"
#import "LLTextFormatterUtils.h"

@implementation UITextField (LLFormatter)

// 适配XIB中的字体
- (void)setFixWidthScreenFont:(float)fixWidthScreenFont
{
    if (fixWidthScreenFont > 0) {
        self.font = [UIFont systemFontOfSize:fixWidthScreenFont];
    } else {
        self.font = self.font;
    }
    
}

- (float)fixWidthScreenFont
{
    return self.fixWidthScreenFont;
}

- (void)setPlaceholder:(NSString *)placeholder lightColor:(nullable UIColor *)lightColor darkColor:(nullable UIColor *)darkColor
{
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:placeholder];
    
    if (lightColor == nil) {
        lightColor = UIColorFromRGB(999999);
    }
    if (darkColor == nil) {
        darkColor = UIColorFromRGB(999999);
    }
    
    UIColor *dyColor = lightColor;
    if (@available(iOS 13.0, *)) {
        dyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {

            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                // 深色模式
                return darkColor;
            } else {
                return lightColor;
            }
        }];
    }

    [attr addAttributes:@{NSForegroundColorAttributeName : dyColor} range:NSMakeRange(0, placeholder.length)];
    
    self.attributedPlaceholder = attr;
    
}

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [LLMethodSwizzTool methodSwizzWithClass:self OriginSEL:@selector(init) swizzSEL:@selector(ll_init)];
        [LLMethodSwizzTool methodSwizzWithClass:self OriginSEL:@selector(initWithFrame:) swizzSEL:@selector(ll_initWithFrame:)];
    });
}

- (instancetype)ll_init
{
    UITextField *tf = [self ll_init];
    tf.delegate = self;
    return tf;
}

- (instancetype)ll_initWithFrame:(CGRect)frame
{
    UITextField *tf = [self ll_initWithFrame:frame];
    tf.delegate = self;
    return tf;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@""]) return YES;
    
    // 限制emoji表情输入
//    if (![textField.placeholder containsString:@"备注"]) {
//        if ([[textField.textInputMode primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) {
//            return NO;
//        }
//    }
    
    if (self.placeholder.length == 0) {
        return YES;
    }
    
    if ([self.placeholder rangeOfString:@"金额"].location != NSNotFound) {
        NSString *money = [textField.text stringByAppendingString:string];
        // 最多输入小数点后两位
        NSString *regex = @"\\d{1,8}\\.\\d{0,2}";
        NSString *regex1 = @"\\d{1,8}";

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
        return  [predicate evaluateWithObject:money] || [predicate1 evaluateWithObject:money];
    } else if ([self.placeholder rangeOfString:@"手机号"].location != NSNotFound) {
        return [LLTextFormatterUtils textField:textField range:range string:string CharType:CharsType_numeral maxLength:11 separatorIndexs:@[@3, @8] separator:@" "];
    } else if ([self.placeholder rangeOfString:@"银行卡"].location != NSNotFound) {
        return [LLTextFormatterUtils textField:textField range:range string:string CharType:CharsType_numeral maxLength:19 separatorIndexs:@[@4, @9, @14, @19] separator:@" "];
    } else if ([self.placeholder rangeOfString:@"身份证"].location != NSNotFound) {
        return textField.text.length < 18;
    } else if ([self.placeholder rangeOfString:@"验证码"].location != NSNotFound) {
        // 验证码
        NSString *phoneNum = [textField.text stringByAppendingString:string];
        NSString *regex = @"\\d{0,4}";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [predicate evaluateWithObject:phoneNum];
    }

    return YES;
}

@end
